//
//  FZHPopView.m
//  test
//
//  Created by 冯志浩 on 16/5/27.
//  Copyright © 2016年 FZH. All rights reserved.
//

#import "FZHPopView.h"
#import "AYTCustomCell.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface FZHPopView ()<UITableViewDataSource,UITableViewDelegate,AYTCustomCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@end
@implementation FZHPopView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        //初始化各种起始属性
        [self initAttribute];
        
        [self initTabelView];
    }
    return self;
}
static NSString * cellID = @"AYTCustomCell";

- (void)initTabelView{
 
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.contentShift) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"AYTCustomCell" bundle:nil] forCellReuseIdentifier:cellID];
    [self.contentView addSubview:self.tableView];
}

/**
 *  初始化起始属性
 */
- (void)initAttribute{
    
    self.buttonH = SCREEN_HEIGHT * (40.0/736.0);
    self.buttonMargin = 10;
    self.contentShift = SCREEN_HEIGHT * (300.0/736.0);
    self.animationTime = 0.8;
    self.backgroundColor = [UIColor colorWithWhite:0.614 alpha:0.700];
    
    [self initSubViews];
}
/**
 *  初始化子控件
 */
- (void)initSubViews{
    
    self.contentView = [[UIView alloc]init];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.contentShift);
    [self addSubview:self.contentView];
}
/**
 *  展示pop视图
 *
 *  @param array 需要显示button的title数组
 */
- (void)showThePopViewWithArray:(NSMutableArray *)array{
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
    
    [window addSubview:self];
    self.dataSource = array;
    //1.执行动画
    [UIView animateWithDuration:self.animationTime animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        self.contentView.transform = CGAffineTransformMakeTranslation(0, -self.contentShift);
    }];
    
}

- (void)dismissThePopView{
    
    [UIView animateWithDuration:self.animationTime animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        self.contentView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissThePopView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AYTCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    cell.customCellDelegate = self;
    return cell;
}
#pragma mark - UITableViewDelagate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.contentShift - 20;
}


- (void)clickButton:(UIButton *)button andView:(UIView *)view
{
    if (button.tag == 88) {//点击了叉叉
        [self dismissThePopView];
    } else if (button.tag == 99) {//点击了去付款
        if ([self.fzhPopViewDelegate respondsToSelector:@selector(getTheButtonTitleWithView:)]) {
            [self.fzhPopViewDelegate getTheButtonTitleWithView:view];
        }
    }
}
@end
